//
//  MainWindowController.m
//  test-NSAnimation
//
//  Created by 杨岑 on 1/11/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "MainWindowController.h"
#import "MyAnimationGroupItem.h"
#import "MyAnimationGroup.h"


@interface MainWindowController ()
@property (weak) IBOutlet NSWindow* testWindow;
@end

@implementation MainWindowController

- (IBAction)test_click:(id)sender
{
    static MyAnimationGroup* group;
    if (group.playing) return;
    
    NSRect rect0 = NSMakeRect(550, 550, 0, 0);
    NSRect rect1 = NSMakeRect(550, 550, 200, 200);
    NSRect rect2 = NSMakeRect(530, 550, 240, 220);
    NSValue* startFrame = [NSValue valueWithRect:rect0];
    NSValue* endFrame = [NSValue valueWithRect:rect1];
    NSValue* popFrame = [NSValue valueWithRect:rect2];
    
    group = [[MyAnimationGroup alloc] init];
    
    NSDictionary* popProps = @{NSViewAnimationTargetKey: _testWindow,
                            NSViewAnimationStartFrameKey: startFrame,
                            NSViewAnimationEndFrameKey: popFrame};
    NSViewAnimation* anim = [[NSViewAnimation alloc] initWithViewAnimations:@[popProps]];
    anim.duration = .2;
    anim.animationBlockingMode = NSAnimationNonblocking;
    //anim.animationCurve = NSAnimationLinear;
    
    MyAnimationGroupItem* item = [[MyAnimationGroupItem alloc] init];
    item.name = @"pop";
    item.animation = anim;
    item.onStart = ^(MyAnimationGroupItem* item) {
        [_testWindow setFrame:rect0 display:YES];
        _testWindow.isVisible = YES;
        [_testWindow makeKeyAndOrderFront:self];
    };
    [group addItem:item];
    
    NSDictionary* shrinkProps = @{NSViewAnimationTargetKey: _testWindow,
                            NSViewAnimationStartFrameKey: popFrame,
                            NSViewAnimationEndFrameKey: endFrame};
    anim = [[NSViewAnimation alloc] initWithViewAnimations:@[shrinkProps]];
    anim.duration = .05;
    anim.animationBlockingMode = NSAnimationNonblocking;
    //anim.animationCurve = NSAnimationEaseIn;
    
    item = [[MyAnimationGroupItem alloc] init];
    item.name = @"shrink";
    item.animation = anim;
    item.onStart = ^(MyAnimationGroupItem* item) {
        NSLog(@"onStart: %@", item);
    };
    [group addItem:item];
    
    NSDictionary* fadeOutProps = @{NSViewAnimationTargetKey: _testWindow,
                            NSViewAnimationEffectKey: NSViewAnimationFadeOutEffect};
    anim = [[NSViewAnimation alloc] initWithViewAnimations:@[fadeOutProps]];
    anim.duration = .8;
    anim.animationBlockingMode = NSAnimationNonblocking;
    //anim.animationCurve = NSAnimationEaseIn;
    
    item = [[MyAnimationGroupItem alloc] init];
    item.name = @"fade out";
    item.animation = anim;
    item.onStart = ^(MyAnimationGroupItem* item) {
        NSLog(@"onStart: %@", item);
        _testWindow.alphaValue = 1.0;
    };
    item.onFinished = ^(MyAnimationGroupItem* item) {
        _testWindow.isVisible = NO;
        _testWindow.alphaValue = 1.0;
        NSLog(@"onStart: %@", item);
    };
    [group addItem:item];
    
    [group start];
}

@end
